package com.Smart.smartscals.Activity;

import java.util.ArrayList;

import com.Smart.smartscals.Adapter.MainViewPagerAdapter;
import com.Smart.smartscals.Adapter.SlideAdapter;
import com.Smart.smartscals.Entity.EntityListView;
import com.Smart.smartscals.Fragment.BaseFragment;
import com.Smart.smartscals.Fragment.FragmentMainName;
import com.Smart.smartscals.Fragment.FragmentMainReport;
import com.Smart.smartscals.Fragment.FragmentMainSet;
import com.Smart.smartscals.Fragment.FragmentMainTarget;
import com.Smart.smartscals.Utils.Const;
import com.Smart.smartscals.Utils.Tools;
import com.slidingmenu.lib.SlidingMenu;
//import com.slidingmenu.lib.SlidingMenu;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ActivityMain extends BaseActivity implements OnClickListener {

	private ArrayList<BaseFragment> arrayListFragment;
	private ViewPager mViewPager;
	private FragmentManager mFragmentManager;
	private FragmentTransaction mFragmentTransaction;
	

	private MainViewPagerAdapter mViewPagerAdapter;

	private FragmentMainName fragmentMainName;
	private FragmentMainReport fragmentMainReport;
	private FragmentMainSet fragmentMainSet;
	private FragmentMainTarget fragmentMainTarget;
	// 动画
	private View lineViewAni;
	private int posOne, posSec, posThir, posFouth;
	// 记录上一次选择的页面；
	private int upIndex, index;

	private long secondPressed;

	private TextView tvTopName, tvTopReport, tvTopTaget, tvTopSet;
	// 侧滑相关的东西；
	private SlidingMenu menu;
	private View slideView;
	private ListView mSliListView;
	private SlideAdapter mSlideAdapter;
	private ArrayList<EntityListView> arrayListViewsUserInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initParam();
		setListener();
	}

	// 初始化宽度，滑动动画的，距离,甚至滑动条的宽度；
	private void initWidth() {
		lineViewAni = findViewById(R.id.main_anima_line);

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;
		posOne = (int) (screenW / 4.0);
		posSec = posOne * 2;
		posThir = posOne * 3;
		posFouth = posOne * 4;

		Tools.setLog("--->0--anir:" + posOne);
	}

	private void setListener() {
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				upIndex = index;
				index = arg0;
				statLineAnimation();
				Tools.setLog("--->1--anir:" + arg0);
				// 设置SlidMenu是否可以触发；
				if (arg0 == 0) {
					menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
				} else {
					menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

				Tools.setLog("--->2--anir:" + arg0);
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

				Tools.setLog("--->3--anir:" + arg0);
			}
		});
		tvTopName.setOnClickListener(this);
		tvTopReport.setOnClickListener(this);
		tvTopSet.setOnClickListener(this);
		tvTopTaget.setOnClickListener(this);

	}

	/*
	 * 开始动画
	 */
	protected void statLineAnimation() {
		Animation animation = null;
		if (index > upIndex) {// 动画右边
			animation = new TranslateAnimation(upIndex * posOne,
					index * posOne, 0, 0);
		} else if (index < upIndex) {// 动画左边；
			animation = new TranslateAnimation(upIndex * posOne,
					index * posOne, 0, 0);
		}
		if (animation != null) {
			animation.setDuration(200);
			animation.setFillAfter(true);
			lineViewAni.startAnimation(animation);
		}

	}

	/*
	 * 初始化一些参数;
	 */
	private void initParam() {
		arrayListViewsUserInfo = new ArrayList<EntityListView>();
		EntityListView entityListView0 = new EntityListView(
				R.drawable.family_guest, "huangbin30744", null, null);
		arrayListViewsUserInfo.add(entityListView0);
		EntityListView entityListView1 = new EntityListView(
				R.drawable.family_guest, "uu12dd", null, null);
		arrayListViewsUserInfo.add(entityListView1);
		EntityListView entityListView2 = new EntityListView(
				R.drawable.family_guest, "rr3033", null, null);
		arrayListViewsUserInfo.add(entityListView2);
		mSlideAdapter = new SlideAdapter(mSliListView, arrayListViewsUserInfo,
				mContext);

		// Fragment的初始化
		fragmentMainName = new FragmentMainName();
		fragmentMainReport = new FragmentMainReport();
		fragmentMainSet = new FragmentMainSet();
		fragmentMainTarget = new FragmentMainTarget();

		// 添加Frament
		arrayListFragment = new ArrayList<BaseFragment>();
		arrayListFragment.add(fragmentMainName);
		arrayListFragment.add(fragmentMainReport);
		//
		// arrayListFragment.add(fragmentMainTarget);
		arrayListFragment.add(fragmentMainTarget);
		arrayListFragment.add(fragmentMainSet);

		mFragmentManager = this.getSupportFragmentManager();
		mFragmentTransaction = mFragmentManager.beginTransaction();

		mViewPagerAdapter = new MainViewPagerAdapter(mFragmentManager,
				arrayListFragment);

		mViewPager.setAdapter(mViewPagerAdapter);
		initWidth();

		setLeftMenu();

	}

	private void setLeftMenu() {

		// configure the SlidingMenu
		menu = new SlidingMenu(this);
		menu.setMode(SlidingMenu.LEFT);
		// 设置触摸屏幕的模式
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		 menu.setShadowWidthRes(R.dimen.dp_10dp);
		// menu.setShadowDrawable(R.drawable.shadow);

		// 设置滑动菜单视图的宽度
		menu.setBehindWidth(this.getWindowManager().getDefaultDisplay()
				.getWidth() * 2 / 3);
		// 设置渐入渐出效果的值
		menu.setFadeDegree(0.35f);

		/**
		 * SLIDING_WINDOW will include the Title/ActionBar in the content
		 * section of the SlidingMenu, while SLIDING_CONTENT does not.
		 */
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		// 为侧滑菜单设置布局
		menu.setMenu(slideView);

		mSliListView.setAdapter(mSlideAdapter);
	}

	/*
	 * 初始化一些View;
	 */
	private void initView() {
		setContentView(R.layout.activitymain);
		mViewPager = (ViewPager) this.findViewById(R.id.main_viewpager);

		tvTopName = (TextView) this.findViewById(R.id.main_tv_name);
		tvTopReport = (TextView) this.findViewById(R.id.main_tv_report);
		tvTopTaget = (TextView) this.findViewById(R.id.main_tv_target);
		tvTopSet = (TextView) this.findViewById(R.id.main_tv_set);
		slideView = LayoutInflater.from(mContext).inflate(
				R.layout.main_slide_left, null);
		mSliListView = (ListView) slideView
				.findViewById(R.id.main_slide_listview);
	}

	@Override
	public void onBackPressed() {

		if (System.currentTimeMillis() - secondPressed <= Const.PRESSEDTIME) {

			super.onBackPressed();
		} else {
			Tools.setToast(this,
					this.getString(R.string.main_toast_second_exit));
			secondPressed = System.currentTimeMillis();
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.main_tv_name:
			mViewPager.setCurrentItem(0);
			break;
		case R.id.main_tv_report:
			mViewPager.setCurrentItem(1);
			break;
		case R.id.main_tv_target:
			mViewPager.setCurrentItem(2);
			break;
		case R.id.main_tv_set:
			mViewPager.setCurrentItem(3);
			break;

		default:
			break;
		}
	}
	public FragmentManager getmFragmentManager() {
		return mFragmentManager;
	}

	public void setmFragmentManager(FragmentManager mFragmentManager) {
		this.mFragmentManager = mFragmentManager;
	}
}
